package com.mlegrand.firestarter.util.write
{
	
	
	import com.mlegrand.firestarter.util.ReplaceUtil;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
		
	public class WriteNavFiles
	{
		
		protected var _location:String;
		protected var _projectName:String;
		protected var _clientName:String;
		protected var _sections:Array;
		
				
		
		public function WriteNavFiles(location:String, projectName:String, clientName:String, sections:Array)
		{
			_location = location
			_projectName = projectName;
			_clientName = clientName;
			_sections = sections;
		}
		
		
		public function write():void
		{
			writeApplicationHeaderBase();
			writeNavManager();
			writeApplicationHeader();
		}
		
		protected function writeApplicationHeaderBase():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/com/'+ _clientName.toLowerCase()+'/'+_projectName.toLowerCase()+'/view/header/ApplicationHeaderBase.mxml');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				while(t.charAt(0)!= '<')
				{
					t = t.slice(1, t.length);
				}
				
				//add Buttons
				for each (var o:Object in _sections)
				{
					var name:String = o.toString();
					var camel:String = name.charAt(0).toLowerCase() + name.slice(1,name.length);

					t = t.slice(0, t.length-11)+ 
						'		<mx:Button id="'+camel+'Btn" \n' + 
						'				label="'+name+'" \n' + 
						'				styleName="applicatoinHeaderButtonStyle"\n' + 
						'				click="'+camel+'BtnClickHandler(event)"/> \n'+
						t.slice(t.length-11, t.length);
				}
				
				
				var scriptPlacement:int = t.indexOf("// event handlers")+100;
				
				//add event handlers
				for each (var o2:Object in _sections)
				{
					var name2:String = o2.toString();
					var camel2:String = name2.charAt(0).toLowerCase() + name2.slice(1,name2.length);

					t = t.slice(0, scriptPlacement)+ 
						'' + '\n'+
						'			/**' + '\n'+
						'			 * Handles the '+camel2+'Btn click event. ' + '\n'+
						'			 * Earmarked for override.' + '\n'+
						'			 * ' + '\n'+
						'			 * @param		event		MouseEvent' + '\n'+
						'			 */ ' + '\n'+
						'			protected function '+camel2+'BtnClickHandler(event:MouseEvent):void' + '\n'+
						'			{' + '\n'+
						'				trace("This method ('+camel2+'BtnClickHandler) is earmarked to be overridden.");' + '\n'+
						'			}'+  '\n'+
						t.slice(scriptPlacement, t.length);
				}
							
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/nav/ApplicationHeaderBase.txt"));
		}
		
		
		protected function writeApplicationHeader():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				t = writeInOverridenEventHandler(t);
				t = replaceDoubleDots(t);
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/com/'+ _clientName.toLowerCase()+'/'+_projectName.toLowerCase()+'/view/header/ApplicationHeader.mxml');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/nav/ApplicationHeader.txt"));
		}
		
		protected function writeInOverridenEventHandler(t:String):String
		{
			var scriptPlacement:int = t.indexOf("// overridden event handlers")+110;
			var overrideSliceText:String = '';
			for (var i:int = 0; i<_sections.length; i++)
			{
				var name:String = _sections[i].toString();
				
				overrideSliceText = overrideSliceText+ '\n' + 
				'			/**'+
			 	'			 * Handles the '+name.toLowerCase()+'Btn click event.\n'+ 
			 	'			 * Overrides the click handler. \n'+
			 	'			 * \n'+
			 	'			 * @param 	event		MouseEvent \n'+
			 	'			 */ \n'+
				'			override protected function '+ReplaceUtil.switchStringToCamelCase(name)+'BtnClickHandler(event:MouseEvent):void \n'+
				'			{\n'+
				'				model.applicationNavigationManager.currentView = ApplicationNavigationManager.'+name.toUpperCase()+'_VIEW; \n'+
				'			}\n';
				
			}
			
			t = t.slice(0, scriptPlacement)+ overrideSliceText + t.slice(scriptPlacement, t.length);
			return t;
		}
		
		protected function replaceDoubleDots(t:String):String
		{
			while (t.indexOf('..')!=-1)
			{
				t = t.replace('..', '.'+_clientName.toLowerCase()+'.'+_projectName.toLowerCase()+'.');
			}
			return t;
		}		
	
		protected function writeNavManager():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var scriptPlacement:int = t.indexOf("// public static consts")+142;
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/com/'+ _clientName.toLowerCase()+'/'+_projectName.toLowerCase()+'/managers/ApplicationNavigationManager.as');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				t = t.slice(0,8)+ 'com.'+_clientName.toLowerCase()+'.'+_projectName.toLowerCase()+'.managers' +t.slice(9,t.length);				
				//add static consts
				var textToSliceIn:String = '';
				for (var i:int = 0; i<_sections.length; i++)
				{
					var name:String = _sections[i].toString();
					textToSliceIn = textToSliceIn+ '		public static const '+name.toUpperCase()+'_VIEW:Number = '+i+';  \n'
					
				}
				t = t.slice(0, scriptPlacement)+ textToSliceIn + t.slice(scriptPlacement, t.length);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/nav/ApplicationNavigationManager.txt"));
		}	
	}
}